var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : "/zofui_sales/card/list",
        tabs : [{name:'全部卡券'},{name:'在线使用'},{name:'到店使用'}],
        page : {
            isend : false,
            doing : false,
            doo : 'card',
            pdata : {
                op : 'list',
                page : 1,
                type : 0,
                isinit : 1,
                sortid : 0,
                ordertype : 0,
                search : '',
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        isshowp : false,
        posterurl : '',
        sortop : [],
        typeop : [],
        orderop : [],
    },
    onLoad:function(options){
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
		var shopid = options.shopid ? options.shopid : 0;
        var sortid = options.sortid ? options.sortid : 0;
        if( options.scene ){
            var list = options.scene.split('_');
            zfid = list[0] ? list[0] : 0;
            sortid = list[1] ? list[1] : 0;
        }
        
        self.setData({
            'page.pdata.zfid' : zfid,
            'page.pdata.shopid' : shopid,
            'page.pdata.sortid' : sortid,
            'page.pdata.isinit' : 1,
            options : options,
        });
        
        app.com.getLocation(self,function(loc){
            self.setData({
                'page.pdata.lng' : loc.longitude,
                'page.pdata.lat' : loc.latitude,
            });
            app.com.auth(function(user){
                app.com.getPage(self,self.data.page,20,function(res){
                    var data = res.data.data;
                    self.setData({
                       datalist : data.list,
                       params : res.data.data.params,
                       sharedata : res.data.data.sharedata,
                       sortop : data.sortop,
                       typeop : data.typeop,
                       orderop : data.orderop,
                       'page.pdata.isinit' : 0,
                    },function(){
                        self.setData({
                            inited : true,
                        });
                    });

                    wx.setNavigationBarTitle({
                        title : '领券中心',
                    });

                },false,function(res){
                    app.com.alert(res.data.message);
                });
				app.com.setBar(app,self,shopid);
            });
        })
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
            'page.pdata.page' : 1,
            'page.isend' : false,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    switch : function(e){
        var self = this;

        if( self.data.ising || e.target.dataset.status == self.data.page.pdata.status  ) return false;
        self.data.ising = true;

        self.setData({
            'page.pdata.type' : e.currentTarget.dataset.status,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });

        self.data.ising = false;
    },
    changesort(e){
        var self = this;

        if( self.data.ising || ( e.currentTarget.dataset.id == self.data.page.pdata.sortid ) ) return false;
        self.data.ising = true;

        self.setData({
            'page.pdata.sortid' : e.currentTarget.dataset.id,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });

        self.data.ising = false;
    },
    changepak(e){
        var self = this;
        var value = e.detail;
        var type = e.currentTarget.dataset.type;

        if( self.data.ising ) return false;
        self.data.ising = true;
        self.setData({
            ['page.pdata.'+type] : value,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
        self.data.ising = false;
    },
    useit(e){
        var self = this;
        app.com.http('cut','POST',{cid:e.currentTarget.dataset.id,op:'getoid'},0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                var tourl = '/zofui_sales/order/orderinfo?id='+res.data.data.orderid;
                wx.navigateTo({
                  url: tourl
                });
            }else{
                app.com.alert(res.data.message);
            }
        })
    },
    searchfn(e){
        var self = this;
        if(!e) return false;
        self.setData({
            datalist : [],
            'page.pdata.page' : 1,
            'page.pdata.search' : e.detail,
            'page.isend' : false,
        });
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    clearsearch(){
        var e = {
            detail : 0,
        };
        this.searchfn(e);
    },
    payit(e){
        // 4 砍价订单
        wx.setStorageSync('zfssbuy',{'cid':e.currentTarget.dataset.id,type:4});
        var tourl = '/zofui_sales/order/confirm';
        wx.navigateTo({
          url: tourl
        });
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    shareit(){
        this.setData({
            isshowp : !this.data.isshowp
        })
    },
    getPoster(){
        var self = this;
        var postdata = {
            op : 'card',
            type : 'list',
            path : self.data.sharedata.path,
        };
        app.com.http('poster','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data ) {
                self.setData({
                    posterurl : res.data.data.url
                })
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    saveposter(){
        var self = this;
        wx.downloadFile({
            url: self.data.posterurl,
            success: function (res) {
                wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                    app.com.toast('已保存到本地相册');
                },
                fail(res) {
                    app.com.toast('保存失败');
                }
              })
            },
            fail: function (res) {
                app.com.toast('下载文件失败');
            }
        })
    },
    location : function( e ){
        app.com.location( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e);
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
    

})